<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<html>
<head>
    <title>心理咨询详情 - 教育新闻网</title>
    <link href="${pageContext.request.contextPath}/static/css/bootstrap.min.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/static/css/bootstrap-icons.css" rel="stylesheet">
    <style>
        .post-detail {
            background: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,.05);
            margin: 30px 0;
        }
        .post-title {
            font-size: 24px;
            color: #333;
            margin-bottom: 20px;
        }
        .post-meta {
            color: #666;
            font-size: 14px;
            margin-bottom: 20px;
            padding-bottom: 20px;
            border-bottom: 1px solid #eee;
        }
        .post-meta span {
            margin-right: 20px;
        }
        .post-content {
            color: #333;
            line-height: 1.8;
            margin-bottom: 30px;
        }
        .reply-section {
            margin-top: 40px;
        }
        .reply-title {
            font-size: 20px;
            color: #333;
            margin-bottom: 20px;
        }
        .reply-item {
            background: #f8f9fa;
            padding: 20px;
            border-radius: 10px;
            margin-bottom: 20px;
        }
        .reply-meta {
            color: #666;
            font-size: 14px;
            margin-bottom: 10px;
        }
        .reply-content {
            color: #333;
            line-height: 1.6;
        }
        .reply-form {
            background: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,.05);
            margin-top: 30px;
        }
        .reply-count {
            font-size: 14px;
            color: #666;
            background: #f8f9fa;
            padding: 4px 12px;
            border-radius: 15px;
        }
        .reply-item:hover {
            transform: translateY(-2px);
            box-shadow: 0 2px 8px rgba(0,0,0,.05);
        }
        .reply-meta .user-info {
            display: flex;
            align-items: center;
        }
        .reply-meta .counselor-tag {
            background: #198754;
            color: white;
            padding: 2px 8px;
            border-radius: 4px;
            font-size: 12px;
            margin-left: 10px;
        }
        .reply-form h4 {
            color: #333;
            margin-bottom: 20px;
            padding-bottom: 15px;
            border-bottom: 1px solid #eee;
        }
        .back-link {
            display: inline-flex;
            align-items: center;
            color: #666;
            text-decoration: none;
            margin-bottom: 20px;
            padding: 8px 16px;
            background: white;
            border-radius: 20px;
            transition: all 0.3s;
        }
        .back-link:hover {
            color: #0d6efd;
            background: #f8f9fa;
            transform: translateX(-5px);
        }
        .back-link i {
            margin-right: 5px;
        }
        .alert-warning {
            border: none;
            background: #fff3cd;
            color: #664d03;
            border-radius: 8px;
        }
        .alert-warning .alert-link {
            color: #664d03;
            text-decoration: underline;
        }
        .form-control {
            border-radius: 8px;
            border: 1px solid #dee2e6;
            padding: 12px;
        }
        .form-control:focus {
            box-shadow: 0 0 0 0.25rem rgba(13,110,253,.15);
        }
        .btn-primary {
            padding: 8px 20px;
            border-radius: 6px;
        }
        .status-tag {
            display: inline-block;
            padding: 4px 12px;
            border-radius: 15px;
            font-size: 12px;
            margin-left: 10px;
        }
        .status-waiting {
            background: #fff3cd;
            color: #664d03;
        }
        .status-replied {
            background: #d1e7dd;
            color: #0f5132;
        }
    </style>
</head>
<body class="bg-light">
    <!-- 引入头部 -->
    <jsp:include page="/user/common/header.jsp"/>
    
    <div class="container">
        <a href="${pageContext.request.contextPath}/user/consult" class="back-link">
            <i class="bi bi-arrow-left"></i> 返回咨询列表
        </a>
        
        <div class="post-detail">
            <h1 class="post-title">
                ${post.title}
                <span class="status-tag ${replyList.size() > 0 ? 'status-replied' : 'status-waiting'}">
                    ${replyList.size() > 0 ? '已回复' : '等待回复'}
                </span>
            </h1>
            <div class="post-meta">
                <span><i class="bi bi-person"></i> ${post.username}</span>
                <span><i class="bi bi-clock"></i> <fmt:formatDate value="${post.create_time}" pattern="yyyy-MM-dd HH:mm"/></span>
                <span><i class="bi bi-eye"></i> ${post.view_count}次浏览</span>
                <span><i class="bi bi-chat-dots"></i> ${replyList.size()}条回复</span>
            </div>
            <div class="post-content">
                ${post.content}
            </div>
        </div>

        <div class="reply-section">
            <div class="reply-title">
                <span>回复列表</span>
                <span class="reply-count">${replyList.size()}条回复</span>
            </div>
            
            <c:forEach items="${replyList}" var="reply">
                <div class="reply-item">
                    <div class="reply-meta">
                        <div class="user-info">
                            <span><i class="bi bi-person"></i> ${reply.username}</span>
                            <c:if test="${reply.role_id == 1}">
                                <span class="counselor-tag">心理咨询师</span>
                            </c:if>
                        </div>
                        <span><i class="bi bi-clock"></i> <fmt:formatDate value="${reply.create_time}" pattern="yyyy-MM-dd HH:mm"/></span>
                    </div>
                    <div class="reply-content">
                        ${reply.content}
                    </div>
                </div>
            </c:forEach>

            <c:if test="${empty replyList}">
                <div class="text-center py-5">
                    <img src="${pageContext.request.contextPath}/static/images/empty.svg" alt="暂无回复" style="width: 150px; margin-bottom: 20px;">
                    <p class="text-muted">暂无回复，请耐心等待咨询师回复</p>
                </div>
            </c:if>

            <div class="reply-form">
                <h4>发表回复</h4>
                <c:choose>
                    <c:when test="${empty sessionScope.user}">
                        <div class="alert alert-warning">
                            请先<a href="${pageContext.request.contextPath}/user/login.jsp" class="alert-link">登录</a>后再回复
                        </div>
                    </c:when>
                    <c:otherwise>
                        <form id="replyForm">
                            <div class="mb-3">
                                <textarea class="form-control" name="content" rows="4" required
                                    placeholder="请输入回复内容..."></textarea>
                            </div>
                            <button type="button" class="btn btn-primary" onclick="submitReply()">
                                <i class="bi bi-send"></i> 发表回复
                            </button>
                        </form>
                    </c:otherwise>
                </c:choose>
            </div>
        </div>
    </div>
    
    <!-- 引入底部 -->
    <jsp:include page="/user/common/footer.jsp"/>
    
    <script src="${pageContext.request.contextPath}/static/js/bootstrap.bundle.min.js"></script>
    <script>
        // 提交回复
        function submitReply() {
            const form = document.getElementById('replyForm');
            const formData = new FormData(form);
            formData.append('postId', '${post.post_id}');
            
            fetch('${pageContext.request.contextPath}/user/consult/reply', {
                method: 'POST',
                body: new URLSearchParams(formData)
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    alert(data.message);
                    window.location.reload();
                } else {
                    alert(data.message);
                }
            })
            .catch(error => {
                console.error('Error:', error);
                alert('回复失败，请稍后重试');
            });
        }
    </script>
</body>
</html> 